package com.zm.demo.core.leetcode;

import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIterNodeList;
import org.junit.Test;

import java.util.*;

/**
 * Created by Administrator on 2020/1/7.
 */
public class T {

    public int singleNumber(int[] nums) {
        Map<Integer,Integer> map = new HashMap();
        for(int i = 0 ; i < nums.length; i++){
            map.put(nums[i], map.get(nums[i]) == null ? 1 : (map.get(nums[i])+1));
        }

        for(Map.Entry<Integer, Integer> e : map.entrySet()){
            if(e.getValue() == 1){
                return e.getKey();
            }
        }
        return 0;
    }


    public void merge(int[] A, int m, int[] B, int n) {
        int k=m+n-1;
        for(int i = m-1, j = n-1; ;){
            if (k<0 || j<0){
                break;
            }

            if (i<0){
                A[k--] = B[j--];
            }else if (A[i] >= B[j]){
                A[k--] = A[i--];
            }else{
                A[k--] = B[j--];
            }
        }
    }

    public int[] subSort(int[] array) {
        //如果中途没有i+1的元素小于i的元素，则为全部升序
        //标记已遍历的最大值，标记最小值。
        if(array == null || array.length == 0){
            return new int[]{-1,-1};
        }
        int length = array.length;
        int maxIndex = -1;
        int maxValue =array[0];
        int minValue = Integer.MIN_VALUE;
        int minIndex = -1;
        for(int i = 1 ; i<length; i++){
            if(array[i] < maxValue){
                maxIndex = i;
                minValue = Math.min(minValue, array[i]);
            }else{
                maxValue = Math.max(maxValue, array[i]);
            }
        }

        for(int i = 0 ; i<= maxIndex; i++){
            if(array[i] > minValue){
                minIndex = i;
                break;
            }
        }
        LinkedList<Integer> in = new LinkedList<>();
        return new int[]{minIndex, maxIndex};
    }

    @Test
    public void test111(){
        int a = 'a' - 'b';
        System.out.println(a);
    }


    public List<String> commonChars(String[] A) {
        int length = A.length;
        int []zimu = new int[26];

        Map<Character, Character> map = new HashMap<>();
        for(int i = 0; i < length; i++){
            int l1 = A[i].length();
            map.clear();
            for(int j = 0; j < l1; j++){
                char c = A[i].charAt(j);
                if(map.putIfAbsent(c,c) == null){
                    zimu[c - 'a']++;
                }
            }
        }

        List<String> list = new ArrayList<>();

        for(int i = 0; i<26; i++){
            if(zimu[i] >= 3){
                list.add(String.valueOf((char)('a'+i)));
            }
        }
        return list;
    }
}

